package com.nbsaas.boot.queue.controller;

import com.nbsaas.boot.queue.scheduler.TaskScheduler;
import com.nbsaas.boot.queue.task.Task;
import com.nbsaas.boot.queue.task.TaskStatus;
import com.nbsaas.boot.queue.task.example.PrintTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/tasks")
public class TaskController {
    @Autowired
    private TaskScheduler taskScheduler;

    @PostMapping
    public Task scheduleTask(@RequestParam String message, @RequestParam int delaySeconds) {
        LocalDateTime executeTime = LocalDateTime.now().plusSeconds(delaySeconds);
        PrintTask task = new PrintTask(message, executeTime);
        taskScheduler.scheduleTask(task);
        return task;
    }

    @GetMapping("/demoTask")
    public Task demoTask(String message) {

        LocalDateTime executeTime = LocalDateTime.now().plusSeconds(10);
        PrintTask task = new PrintTask(message, executeTime);
        taskScheduler.scheduleTask(task);
        return task;
    }

    @GetMapping("/{taskId}")
    public Task getTask(@PathVariable String taskId) {
        return taskScheduler.getTask(taskId);
    }

    @GetMapping("/status/{status}")
    public List<Task> getTasksByStatus(@PathVariable TaskStatus status) {
        return taskScheduler.getTasksByStatus(status);
    }

    @GetMapping("/statistics")
    public Object getStatistics() {
        return taskScheduler.getStatistics();
    }

    @DeleteMapping("/{taskId}")
    public void cancelTask(@PathVariable String taskId) {
        taskScheduler.cancelTask(taskId);
    }
} 